<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class IndexController extends BaseController {
    /**
     **首页
     **/
    public function index(){
        $this->check_login();


        $userid = $_SESSION['userid'];

//        //我的奖品
        $prize_list = S('my_prize_list_'.$userid);
        if(!$prize_list){
            $prize_list = M('myprize') ->  where('fk_user_id=%d',$_SESSION['userid'])->field('pk_id,phone,prize_name') -> select();
            S('my_prize_list_'.$userid , $prize_list);
        }
        $data['my_prize_list'] = $prize_list;

//        $data['rest'] = $this->getPlayNumber();

        //输出签名
        $Weixin = new \Weixin\Controller\WeixinFuwuController();
        $data['wxKey'] = $Weixin->rtnQM();
        $this -> showView($data,'./Public/Index.html');

    }


    /**
     **用户登录
     **/
    public function login($success_url){
        !$_GET['code'] && $this ->rtnResult(77,'非法操作');
        $uInfo = $this -> oauth2_user_info($_GET['code']);
        $_SESSION['uInfo'] = $uInfo;

        //用户是否已经存在
        $User = M('User');
        $info = $User -> where("openid='%s'",$uInfo['openid']) -> field('pk_id,nickname,headimgurl') -> find();
        if(empty($info)){
            //加入到数据库
            $data['openid'] = $uInfo['openid'];
            $data['nickname'] = $uInfo['nickname'];
            $data['headimgurl'] = $uInfo['headimgurl'];
            $data['sex'] = $uInfo['sex'];
            $data['country'] = $uInfo['country'];
            $data['province'] = $uInfo['province'];
            $data['city'] = $uInfo['city'];
            $uid = $User ->add($data);
            !$uid && $this ->rtnResult(1,'login error');

            //设置可用次数（默认3次）
            S('number_'.$uid , C('FREE_NUM'));
        }
        $_SESSION['headimgurl'] = $info['headimgurl'] ? $info['headimgurl'] : $uInfo['headimgurl'];
        $_SESSION['nickname'] = $info['nickname'] ? $info['nickname'] : $uInfo['nickname'];
        $_SESSION['openid'] = $info['openid'] ? $info['openid'] : $uInfo['openid'];
        $_SESSION['userid'] = $info['pk_id'] ? $info['pk_id'] : $uid;
        if($success_url){
            header('location:'.U('Home/Index/notice').'?way='.$_GET['state']);
        }
    }



    //游戏说明
    public function notice(){
        $this->check_login();
//        $way = getWay();//入口缓存
//        createType(); //创建显示类型

        $data['now_time'] = date('H:i'); //时间
        $data['now_date'] = date('m月d日'); //日期

        $weekarray=array("日","一","二","三","四","五","六");
        $now_week = date('w');
        $data['now_week'] = '星期'.$weekarray[$now_week]; //星期

        $Weixin = new \Weixin\Controller\WeixinFuwuController();
        $data['wxKey'] = $Weixin->rtnQM();


        $this -> showView($data,'./Public/notice.html');
    }

    public function monilogin(){
        $_SESSION['userid'] = 121;
        $_SESSION['openid'] = 'ovma9vqWvjhCTKCHvlUj7TDtiYds';

    }


    /**
     * 删除token
     */
    function del_token(){
        S('access_token',null);
        dump(S('access_token'));
        echo 'success';
    }


    public function gift(){

        $this -> showView('','./Public/gift.html');
    }
    public function advert(){
        $this->check_login();
        $this -> showView('','./Public/advert.html');
    }


    /**
     * 推送菜单
     */
//    public function push_menu(){
//        $menu = array(
//            array(
//                "type" => "view",
//                "name" => "我要报名",
//                "url" => "http://zhangshuzhou.wicp.net/shenhuxi/Home/Index/index"
//            ),
//            array(
//                "type" => "view",
//                "name" => "邀请排行",
//                "url" => "http://zhangshuzhou.wicp.net/shenhuxi/Weixin/Remind/rank"
//            ),
//        );
//
//        $Weixin = new \Weixin\Controller\WeixinFuwuController();
//        $aa = $Weixin->createMenu($menu);
////        dump($aa);
//    }

}